﻿using System;

namespace GenericListForEachByLambda
{
    public class Node<T> { 
        public Node<T> Next { get; set; }
        public T Data { get; set; }
        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }

    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;
        public GenericList()
        {
            tail = head = null;
        }
        public Node<T> Head
        {
            get => head;
        }

        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }
        public void ForEach(Action<T> a)
        {
            Node<T> node = this.head;
            while (node != null)
            {
                a(node.Data);
                node = node.Next;
            }
        }
       
    }
    class Program
    {
        static void Main(string[] args)
        {
            GenericList<int> list = new GenericList<int>();
            Console.WriteLine("Enter the length of the list:");
            int n;
            int number;
            n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("Enter the number:");
                number = Convert.ToInt32(Console.ReadLine());
                list.Add(number);
            }
            Console.WriteLine("print:");
            list.ForEach(x => Console.WriteLine(x));
            int max = 0;
            int min = 0;
            int sum = 0;
            list.ForEach((x) => sum = sum + x);
            Console.WriteLine("The sum:" + sum);
            max = list.Head.Data;
            min = list.Head.Data;
            list.ForEach((x) => { if (max < x) max = x; });
            list.ForEach((x) => { if (min > x) min = x; });
            Console.WriteLine("max:" + max);
            Console.WriteLine("min:" + min);

        }
    }
    }
